package karileet.meeting.profile;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Class contains methods for retrieving/saving app states from/to shared 
 * preferences
 * 
 * @author markus karileet
 */
public class ProfileState {
	public static String PROFILE = "profile";
	public static String VIBRATE = "vibrate";
	public static String START = "start";
	public static String STOP = "stop";
	public static String STARTENABLED = "startenabled";
	public static String STOPENABLED = "stopenabled";
	public static String BEEP = "beep";
	private boolean profileEnabled;
	private boolean vibrateEnabled;
	private int beepVolume;
	private long scheduleStart;
	private long scheduleStop;
	private boolean startEnabled;
	private boolean stopEnabled;
	private Context ctx;
	
	/**
	 * Constructor inits object and populates values from shared preferences
	 * (when existing)
	 * @param ctx
	 */
	public ProfileState(Context ctx) {
		this.ctx = ctx;
		SharedPreferences p = ctx.getSharedPreferences(
				MainActivity.PREFS_NAME, 0);
		this.profileEnabled = p.getBoolean(PROFILE, false);
		this.vibrateEnabled = p.getBoolean(VIBRATE, false);
		this.scheduleStart = p.getLong(START, System.currentTimeMillis());
		this.scheduleStop = p.getLong(STOP, System.currentTimeMillis());
		this.startEnabled = p.getBoolean(STARTENABLED, false);
		this.stopEnabled = p.getBoolean(STOPENABLED, false);
		
		this.beepVolume = p.getInt(BEEP, 100);
	}
	
	/**
	 * @return int Volume of beep
	 */
	public int getBeepVolume() {
		return beepVolume;
	}

	/**
	 * Method stores user defined beep volume
	 * @param int beepVolume Volume of beep
	 */
	public void setBeepVolume(int beepVolume) {
		editPreferences(BEEP, beepVolume);
		this.beepVolume = beepVolume;
	}

	/**
	 * @return boolean whether app is enabled or not
	 */
	public boolean isProfileEnabled() {
		return profileEnabled;
	}
	
	/**
	 * Method stores profile state to shared preferences
	 * @param boolean profileEnabled Whether app is enabled or not
	 */
	public void setProfileEnabled(boolean profileEnabled) {
    	editPreferences(PROFILE, profileEnabled);
		this.profileEnabled = profileEnabled;
	}
	
	/**
	 * @return boolean whether vibrate is enabled or not
	 */
	public boolean isVibrateEnabled() {
		return vibrateEnabled;
	}
	
	/**
	 * Method stores vibrate state to shared preferences
	 * @param boolean vibrateEnabled Whether vibrate is enabled or not
	 */
	public void setVibrateEnabled(boolean vibrateEnabled) {
		editPreferences(VIBRATE, vibrateEnabled);
		this.vibrateEnabled = vibrateEnabled;
	}	
	
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param boolean state State to save
	 */
	private void editPreferences(String type, boolean state) {
		SharedPreferences p = ctx.getSharedPreferences(MainActivity.PREFS_NAME, 0);
		p.edit().putBoolean(type, state).commit();
	}
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param boolean state State to save
	 */
	private void editPreferences(String type, int value) {
		SharedPreferences p = ctx.getSharedPreferences(MainActivity.PREFS_NAME, 0);
		p.edit().putInt(type, value).commit();
	}
	
	public void setScheduleStopEnabled(boolean stopEnabled) {
		editPreferences(STOPENABLED, stopEnabled);
		this.stopEnabled = stopEnabled;
	}
	public void setScheduleStart(long start) {
		SharedPreferences p = ctx.getSharedPreferences(MainActivity.PREFS_NAME, 0);
		p.edit().putLong(START, start).commit();
		this.scheduleStart = start;
	}
	public void setScheduleStop(long stop) {
		SharedPreferences p = ctx.getSharedPreferences(MainActivity.PREFS_NAME, 0);
		p.edit().putLong(STOP, stop).commit();
		this.scheduleStop = stop;
	}
	public long getScheduleStart() {
		return scheduleStart;
	}
	public long getScheduleStop() {
		return scheduleStop;
	}
	
	
	public void setScheduleStartEnabled(boolean startEnabled) {
		editPreferences(STARTENABLED, startEnabled);
		this.startEnabled = startEnabled;
	}
	
	public boolean isScheduleStartEnabled() {
		return startEnabled; 
	}
	
	public boolean isScheduleStopEnabled() {
		return stopEnabled; 
	}
}